/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package th.stripes.resources;

import com.google.inject.Inject;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import net.sourceforge.stripes.action.HandlesEvent;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.StreamingResolution;
import net.sourceforge.stripes.action.UrlBinding;
import org.json.JSONArray;
import org.json.JSONObject;
import th.dao.PengiklananDAO;
import th.manager.AdminManager;
import th.manager.UserManager;
import th.model.InfoAudit;
import th.model.Pengguna;
import th.model.Pengiklanan;
import th.stripes.BaseActionBean;

/**
 *
 * @author fikri
 */
@UrlBinding("/services/admin")
public class AdminResource extends BaseActionBean{
     
     private List<Pengiklanan> senaraiIklan;
     
     @Inject PengiklananDAO pengiklananDAO;
     
     @Inject AdminManager adminManager;
     
     @Inject UserManager user;
     
     
     @HandlesEvent("list")
     public Resolution listAds() {
          senaraiIklan = pengiklananDAO.findAll();
          
          JSONArray array = new JSONArray();
          for (Pengiklanan iklan : senaraiIklan) {
               JSONObject obj = new JSONObject();
               obj.put("idIklan", String.valueOf(iklan.getIdIklan()));
               obj.put("keterangan", iklan.getKeterangan());
               obj.put("aktif", iklan.getAktif());
               array.put(obj);
          }
          return new StreamingResolution("application/json", array.toString());
     }
     
     @HandlesEvent("save_iklan")
     public Resolution saveIklan () {
          
          BufferedReader reader = null;
          String m_error = "";
          try {
               String line = "";
               StringBuilder sb = new StringBuilder();
               reader = getContext().getRequest().getReader();
               while ((line = reader.readLine()) != null) {
                    sb.append(line);
               }

               if (sb.length() > 0) {
                    JSONObject json = new org.json.JSONObject(sb.toString());
                    Pengiklanan iklan = new Pengiklanan();
                    iklan.setIdIklan(Integer.parseInt( (String) json.getString("idIklan")) );
                    iklan.setAktif((String) json.getString("aktif"));
                    iklan.setKeterangan((String)json.getString("keterangan"));
                    
                    String idPengguna = (String) json.getString("userId");
                    Pengguna pengguna = user.findUserByIdUser(idPengguna);
                    
                    InfoAudit ia = new InfoAudit();
                    ia.setDimasukOleh(pengguna);
                    ia.setTarikhMasuk(new Date());     
                    
                    iklan.setInfoAudit(ia);
                    
                    adminManager.saveIklan(iklan);
                    
                    return new StreamingResolution("application/json", "Kemasukan Berjaya.");
               }
          } catch (IOException ex) {
               m_error = ex.getLocalizedMessage();
          } 
         
          return new StreamingResolution("text/plain", m_error);
     }
     
     @HandlesEvent("delete_iklan")
     public Resolution deleteIklan () {
          
          BufferedReader reader = null;
          String m_error = "";
          try {
               String line = "";
               StringBuilder sb = new StringBuilder();
               reader = getContext().getRequest().getReader();
               while ((line = reader.readLine()) != null) {
                    sb.append(line);
               }

               if (sb.length() > 0) {
                    JSONObject json = new org.json.JSONObject(sb.toString());
                    
                    String id = (String) json.getString("id");                   
                    
                    Pengiklanan iklan = pengiklananDAO.findById(Integer.parseInt(id));
                    if (iklan != null) {
                         adminManager.deleteIklan(iklan);
                    
                         return new StreamingResolution("application/json", "Rekod berjaya di padam.");
                    } else {
                         return new StreamingResolution("text/plain", "Rekod tidak dijumpai.");
                    }                    
               }
          } catch (IOException ex) {
               m_error = ex.getLocalizedMessage();
          } 
         
          return new StreamingResolution("text/plain", m_error);
     }
     

     public List<Pengiklanan> getSenaraiIklan() {
          return senaraiIklan;
     }

     public void setSenaraiIklan(List<Pengiklanan> senaraiIklan) {
          this.senaraiIklan = senaraiIklan;
     }
}
